/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : NumberEformProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Nov 27, 2008       Raghu Sarraff			       Created
 */
package net.lwf.handler.math.number;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.entity.QuestionInfo;
import net.lwf.number.util.NumberFormatter;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

/**
 * Type the class description here
 *  
 */
public class NumberExpandFormProcessHandler extends ProcessHandler implements
		IProcessHandler {

	private int NUMBER_START_RANGE = 11;
	private int NUMBER_END_RANGE = 99999;
	
	public NumberExpandFormProcessHandler() {
	}
	
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		return null;
	}
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 */
	public String getQuestionDesc() {
		return null;
	}

	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = generateSingle(true);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return true;
	}


	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}	
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		int number = RandomNumberGenerator.generate(NUMBER_START_RANGE, NUMBER_END_RANGE);
		qinfo.setQuestion(NumberFormatter.format(String.valueOf(number)));

		List<String> lstEpData = this.generateExpandedData(number);
		String answer = "";
		for(int i=0; i<lstEpData.size(); i++) {
			answer += NumberFormatter.format(lstEpData.get(i));
			if(i+1<lstEpData.size()) {
				answer += " + ";
			}
		}
		
		qinfo.setAnswer(answer);
		qinfo.setRadioAnswerChoices(this.getRadioAnswers(answer, lstEpData));
		qinfo.setPlaceHolder("Write the following number in an expanded form.");
		return qinfo;
	}
		
	private List<RadioData> getRadioAnswers(String answer, List<String> lstEpData) {
		List<RadioData> rdata = new ArrayList<RadioData>();
		rdata.add(new RadioData(answer, answer));
		
		//Generate answer choices
		String answer_choice = this.generateAnswerChoice(lstEpData);	
		rdata.add(new RadioData(answer_choice, answer_choice));
		answer_choice = this.generateAnswerChoice(lstEpData);	
		rdata.add(new RadioData(answer_choice, answer_choice));
		answer_choice = this.generateAnswerChoice(lstEpData);	
		rdata.add(new RadioData(answer_choice, answer_choice));
		
		Collections.shuffle(rdata);
		return rdata;
	}
	
	private String generateAnswerChoice(List<String> lstEpData) {
		String answer_choice = "";
		int nCounter = 1;
		for(String data: lstEpData) {
			if(data.length() > 1) {
				int number = RandomNumberGenerator.generate(1, 3);
				if(number == 1) {
					answer_choice += NumberFormatter.format(String.valueOf(Integer.parseInt(data) * 10));
				}
				else if(number == 2) {
					answer_choice += NumberFormatter.format(String.valueOf(Integer.parseInt(data) / 10));
				}
				else {
					answer_choice += NumberFormatter.format(data);
				}
			}
			else {
				answer_choice += NumberFormatter.format(data);
			}
			if(nCounter < lstEpData.size()) {
				answer_choice += " + ";
			}
			nCounter++;
		}
		return answer_choice;
	}
	
	private List<String> generateExpandedData(int number) {
		List<String> lstData = new ArrayList<String>();
		String sNumber = String.valueOf(number);

		int nLen = sNumber.length();
		for(int i=0; i<nLen; i++) {
			String newString = sNumber.substring(i+1, nLen);
			int value = Integer.parseInt(sNumber.substring(i, i+1));
			if(newString != null) {
				String sFactor = "1";
				for(int j=0; j<newString.length(); j++) {
					sFactor += "0";
				}
				value = value * Integer.parseInt(sFactor);
				if(value != 0) {
					lstData.add(String.valueOf(value));
				}
			}
		}
		return lstData;
	}
}

